package store.lunangangster.engine.impl.bpmn.behavior;

import store.lunangangster.bpmn.model.Activity;
import store.lunangangster.bpmn.model.BoundaryEvent;
import store.lunangangster.engine.impl.Context;
import store.lunangangster.engine.impl.entity.ExecutionEntity;
import store.lunangangster.engine.impl.entity.mgr.ExecutionEntityManager;

public abstract class ActivityBehavior extends FlowNodeBehavior {

  @Override
  public void goNext(ExecutionEntity execution) {
    Activity activity = (Activity) execution.getCurrentFlowElement();
    for (BoundaryEvent boundaryEvent : activity.getBoundaryEventList()) {
      ExecutionEntityManager executionManager = Context.getProcessEngineConfiguration()
          .getExecutionEntityManager();
      // create child-execution to execute BoundaryEventBehavior
      ExecutionEntity childExecution = executionManager.createChildExecution(execution);
      childExecution.setScope(false);
      childExecution.setCurrentFlowElement(boundaryEvent);
      childExecution.setStartTime(Context.getProcessEngineConfiguration().getEngineClock().now());

      ((ModelBehavior) boundaryEvent.getBehavior()).doBehavior(childExecution);
    }

    super.goNext(execution);
  }
}
